/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pet;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class AddPetServlet extends HttpServlet {
     public void wrong1(){    //对话框提示信息
        String msg="输入信息不能为空，请重新输入！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
     public void wrong2(){    //对话框提示信息
        String msg="用宠物已存在，添加失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    public void right(){    //对话框提示信息
        String msg="添加成功！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
     protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String ownername=new String(request.getParameter("ownername").getBytes("ISO-8859-1"),"UTF-8");
        String petname=new String(request.getParameter("petname").getBytes("ISO-8859-1"),"UTF-8");
        String type=new String(request.getParameter("type").getBytes("ISO-8859-1"),"UTF-8");
        String birthdate=new String(request.getParameter("birthdate").getBytes("ISO-8859-1"),"UTF-8");
        if(ownername.length()==0||petname.length()==0||type.length()==0||birthdate.length()==0){
            wrong1();
            response.sendRedirect("http://localhost:8084/jspsx/pets/addpet.jsp");
        }
        else{
            try{
                Connection con=null;
                Statement stmt=null,stmt1=null,stmt2=null;
                ResultSet rs=null;
                ResultSet rs1=null;
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/abin? useUnicode=true&characterEncoding=gbk";
                con=DriverManager.getConnection(url,"root","admin");
                stmt=con.createStatement();
                stmt1=con.createStatement();
                stmt2=con.createStatement();
                String sql1="select * from t_pets where name='"+petname+"'";
                rs=stmt.executeQuery(sql1);
                rs.last();
                int k;
                k=rs.getRow();
                if(k>0){
                    wrong2();
                    response.sendRedirect("http://localhost:8084/jspsx/pets/addpet.jsp");
                }else{
                    String sql2="select * from t_pets";
                    rs1=stmt1.executeQuery(sql2);
                rs1.last();
                int petid,n;
                n=rs1.getRow();
                petid=n+1;
                    String sql3="insert into t_pets values("+"'"+petid+"'"+","+"'"+petname+"'"+","+"'"+birthdate+"'"+","+"'"+type+"'"+","+"'"+ownername+"'"+")";
                    stmt2.executeUpdate(sql3);
                    right();
                }
                rs.close();
                stmt.close();
                con.close();
//                right();
//                response.sendRedirect("http://localhost:8084/jspsx/LoginRegister/login.jsp");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
      protected void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        doGet(request,response);
    }

   
}
